package inputWindow;

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;


	/*
	 * This frame is where the user can set the maximum payload size, maximum segment size, and delay between sending each segment
	 */
public class OptionWindow extends JFrame implements ActionListener {

	/*
	 *	Field Declaration 
	 */
	private static final long serialVersionUID = 1L;
	private int _maxPayload = 170;
	private int _segmentSize = 23;
	private int _delay = 0;

	private JPanel contentPane;
	private JTextField payload_field;
	private JTextField segment_field;
	private JTextField delay_field;
	private JButton apply_button;
	private JButton close_button;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					OptionWindow frame = new OptionWindow();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame and all its subcomponents
	 */
	public OptionWindow() {
		setTitle("Options");
		setBounds(100,100,200,140);
		setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		GridBagLayout gbl_contentPane = new GridBagLayout();
		gbl_contentPane.columnWidths = new int[]{0, 0, 0};
		gbl_contentPane.rowHeights = new int[]{0, 0, 0, 0, 0};
		gbl_contentPane.columnWeights = new double[]{0.0, 0.0, Double.MIN_VALUE};
		gbl_contentPane.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		contentPane.setLayout(gbl_contentPane);

		JLabel max_label = new JLabel("Max Payload:");
		GridBagConstraints gbc_max_label = new GridBagConstraints();
		gbc_max_label.anchor = GridBagConstraints.WEST;
		gbc_max_label.insets = new Insets(0, 0, 5, 5);
		gbc_max_label.gridx = 0;
		gbc_max_label.gridy = 0;
		contentPane.add(max_label, gbc_max_label);

		payload_field = new JTextField(Integer.toString(_maxPayload));
		GridBagConstraints gbc_packet_field = new GridBagConstraints();
		gbc_packet_field.fill = GridBagConstraints.HORIZONTAL;
		gbc_packet_field.insets = new Insets(0, 0, 5, 0);
		gbc_packet_field.gridx = 1;
		gbc_packet_field.gridy = 0;
		contentPane.add(payload_field, gbc_packet_field);
		payload_field.setColumns(10);

		JLabel segment_label = new JLabel("Segment Size:");
		GridBagConstraints gbc_segment_label = new GridBagConstraints();
		gbc_segment_label.anchor = GridBagConstraints.WEST;
		gbc_segment_label.insets = new Insets(0, 0, 5, 5);
		gbc_segment_label.gridx = 0;
		gbc_segment_label.gridy = 1;
		contentPane.add(segment_label, gbc_segment_label);

		segment_field = new JTextField(Integer.toString(_segmentSize));
		GridBagConstraints gbc_segment_field = new GridBagConstraints();
		gbc_segment_field.fill = GridBagConstraints.HORIZONTAL;
		gbc_segment_field.insets = new Insets(0, 0, 5, 0);
		gbc_segment_field.gridx = 1;
		gbc_segment_field.gridy = 1;
		contentPane.add(segment_field, gbc_segment_field);
		segment_field.setColumns(10);

		JLabel delay_label = new JLabel("Delay:");
		GridBagConstraints gbc_delay_label = new GridBagConstraints();
		gbc_delay_label.anchor = GridBagConstraints.WEST;
		gbc_delay_label.insets = new Insets(0, 0, 5, 5);
		gbc_delay_label.gridx = 0;
		gbc_delay_label.gridy = 2;
		contentPane.add(delay_label, gbc_delay_label);

		delay_field = new JTextField(Integer.toString(_delay));
		GridBagConstraints gbc_delay_field = new GridBagConstraints();
		gbc_delay_field.fill = GridBagConstraints.HORIZONTAL;
		gbc_delay_field.insets = new Insets(0, 0, 5, 0);
		gbc_delay_field.gridx = 1;
		gbc_delay_field.gridy = 2;
		contentPane.add(delay_field, gbc_delay_field);
		delay_field.setColumns(10);

		close_button = new JButton("Close");
		close_button.addActionListener(this);
		GridBagConstraints gbc_close_button = new GridBagConstraints();
		gbc_close_button.anchor = GridBagConstraints.WEST;
		gbc_close_button.insets = new Insets(0, 0, 0, 5);
		gbc_close_button.gridx = 0;
		gbc_close_button.gridy = 3;
		contentPane.add(close_button, gbc_close_button);

		apply_button = new JButton("Apply");
		apply_button.addActionListener(this);
		GridBagConstraints gbc_apply_button = new GridBagConstraints();
		gbc_apply_button.anchor = GridBagConstraints.EAST;
		gbc_apply_button.gridx = 1;
		gbc_apply_button.gridy = 3;
		contentPane.add(apply_button, gbc_apply_button);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if(e.getSource() == apply_button)									// User applies changes
		{
			_maxPayload = Integer.parseInt(payload_field.getText());		// Sets the maxPayload to user input
			//System.out.println(_maxPayload);

			_segmentSize = Integer.parseInt(segment_field.getText());		// Sets the segmentSize to user input
			//System.out.println(_segmentSize);

			_delay = Integer.parseInt(delay_field.getText());				// Sets the delay to user input
			//System.out.println(_delay);
		}

		if(e.getSource() == close_button)
		{
			this.dispose();
		}
	}

		/*
		 * Returns the maximum payload size, used when the commit button is pressed
		 */
	public int getMaxPayload()
	{
		return _maxPayload;
	}

		/*
		 * Returns the maximum segment size, used when the commit button is pressed
		 */
	public int getSegmentSize()
	{
		return _segmentSize;
	}

		/*
		 * Returns the delay between sending segments, used when the commit button is pressed
		 */
	public int getDelay()
	{
		return _delay;

	}

}
